package com.youlai.quarkus.system.converter;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.quarkus.system.common.model.Option;
import com.youlai.quarkus.system.model.entity.SysRole;
import com.youlai.quarkus.system.model.form.RoleForm;
import com.youlai.quarkus.system.model.vo.RolePageVO;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.ArrayList;
import java.util.List;

@ApplicationScoped
public class RoleConverter {


    public Page<RolePageVO> entity2Page(Page<SysRole> page) {
        if (page == null) {
            return null;
        }

        Page<RolePageVO> page1 = new Page<>();

        page1.setPages(page.getPages());
        page1.setRecords(sysRoleListToRolePageVOList(page.getRecords()));
        page1.setTotal(page.getTotal());
        page1.setSize(page.getSize());
        page1.setCurrent(page.getCurrent());
        page1.setSearchCount(page.searchCount());
        page1.setOptimizeCountSql(page.optimizeCountSql());
        List<OrderItem> list1 = page.orders();
        if (list1 != null) {
            page1.setOrders(new ArrayList<OrderItem>(list1));
        }
        page1.setMaxLimit(page.maxLimit());
        page1.setCountId(page.countId());

        return page1;
    }


    public Option entity2Option(SysRole role) {
        if (role == null) {
            return null;
        }

        Option option = new Option();

        option.setValue(role.getId());
        option.setLabel(role.getName());

        return option;
    }


    public List<Option> entities2Options(List<SysRole> roles) {
        if (roles == null) {
            return null;
        }

        List<Option> list = new ArrayList<Option>(roles.size());
        for (SysRole sysRole : roles) {
            list.add(entity2Option(sysRole));
        }

        return list;
    }


    public SysRole form2Entity(RoleForm roleForm) {
        if (roleForm == null) {
            return null;
        }

        SysRole sysRole = new SysRole();

        sysRole.setId(roleForm.getId());
        sysRole.setName(roleForm.getName());
        sysRole.setCode(roleForm.getCode());
        sysRole.setSort(roleForm.getSort());
        sysRole.setStatus(roleForm.getStatus());
        sysRole.setDataScope(roleForm.getDataScope());

        return sysRole;
    }


    public RoleForm entity2Form(SysRole entity) {
        if (entity == null) {
            return null;
        }

        RoleForm roleForm = new RoleForm();

        roleForm.setId(entity.getId());
        roleForm.setName(entity.getName());
        roleForm.setCode(entity.getCode());
        roleForm.setSort(entity.getSort());
        roleForm.setStatus(entity.getStatus());
        roleForm.setDataScope(entity.getDataScope());

        return roleForm;
    }

    protected RolePageVO sysRoleToRolePageVO(SysRole sysRole) {
        if (sysRole == null) {
            return null;
        }

        RolePageVO rolePageVO = new RolePageVO();

        rolePageVO.setId(sysRole.getId());
        rolePageVO.setName(sysRole.getName());
        rolePageVO.setCode(sysRole.getCode());
        rolePageVO.setStatus(sysRole.getStatus());
        rolePageVO.setSort(sysRole.getSort());
        rolePageVO.setCreateTime(sysRole.getCreateTime());
        rolePageVO.setUpdateTime(sysRole.getUpdateTime());

        return rolePageVO;
    }

    protected List<RolePageVO> sysRoleListToRolePageVOList(List<SysRole> list) {
        if (list == null) {
            return null;
        }

        List<RolePageVO> list1 = new ArrayList<RolePageVO>(list.size());
        for (SysRole sysRole : list) {
            list1.add(sysRoleToRolePageVO(sysRole));
        }

        return list1;
    }

}
